﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using EASerialization.Serialization.MocaTree.Util;
using System.Web;

namespace EASerialization.Serialization.MocaTree
{
    public class MocaAttribute : MocaTreeElement
    {
        public String Value { get; set; }
        public MocaNode Node { get; set; }

        private MocaAttribute(String attributeName, String attributeValue)
        {
            this.Name = attributeName;
            this.Value = attributeValue;
            this.Index = this.Node.Attributes.IndexOf(this);
            if (this.Index == -1)
                this.Index = 0;
        }

        public MocaAttribute() 
        {           
        }


        public override XmlElement serializeToXmlTree(XmlDocument xmlDocument)
        {
            XmlElement attributeNode = xmlDocument.CreateElement(MocaTreeUtil.AttributeNodeDefaultName);
            MocaTreeUtil.appendNewSimpleAttribute(MocaTreeUtil.NameAttributeName, this.Name, attributeNode, xmlDocument);
            MocaTreeUtil.appendNewSimpleAttribute(MocaTreeUtil.ValueAttributeName, this.Value, attributeNode, xmlDocument);
            MocaTreeUtil.appendNewSimpleAttribute(MocaTreeUtil.IndexAttributeName, this.Index.ToString(), attributeNode, xmlDocument);
            
            return attributeNode;
        }

        public override void deserializeFromXmlTree(XmlElement mainNode)
        {
            this.Name = mainNode.Attributes[MocaTreeUtil.NameAttributeName].InnerXml;
            this.Value = HttpUtility.HtmlDecode(mainNode.Attributes[MocaTreeUtil.ValueAttributeName].InnerXml);
            this.Index = int.Parse(mainNode.Attributes[MocaTreeUtil.IndexAttributeName].InnerXml);
        }
    }
}
